/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cluelessserver;

/**
 *
 * @author dcain
 */

import static cluelessserver.CluelessServer.numOfPlayers;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class CluelessServer {

    /**
     * @param args the command line arguments
     */
    //num of players required to start a game
    
    static final int NUMPLAYERSTOSTARTGAME = 3;
    
    static int activeGames = 0;
    static Game[] games = new Game[10];
    
    Socket socket;
    static Connection[] playerPool = new Connection [8];    
    static int numOfPlayers = 0;    
    
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = null;
        boolean listening = true;        
        try {
            serverSocket = new ServerSocket(4444);
        } catch (IOException e) {
            System.err.println("Could not listen on port: 4444.");
            System.exit(-1);
        }
        System.out.println("start listening");
        while (listening){
            Connection w;
            try{
            //server.accept returns a client connection
                w = new Connection(serverSocket.accept());
                Thread t = new Thread(w);               
            } catch (IOException e) {
            System.out.println("Accept failed: 4444");
            System.exit(-1);
            }
        }
    }
   
    
    /**
     * Adds an incoming connection to a pool of connections waiting for a game.
     * 
     * @param c connection to be added to the pool
     * @throws IOException 
     */
    public static void addToPool(Connection c) throws IOException{        
        for (int i = 0; i < 9; i++){            
            if (playerPool[i] != null){
                System.out.println(playerPool[i].getName());
            } else {
                playerPool[i] = c;
                c.isInPool = true;
                numOfPlayers++;
                break;
            }
        }
        checkGame();
    }
    
    /**
     * Checks if there are enough players waiting to start a game and starts one if there are enough.
     * @throws IOException 
     */
    static void checkGame() throws IOException{
        System.out.println("insidecheckgame");
        if (numOfPlayers >= NUMPLAYERSTOSTARTGAME){
            Connection[] gamePlayers = new Connection[NUMPLAYERSTOSTARTGAME];
            int numGamePlayer = 0;            
            for (int i = 0; numGamePlayer < NUMPLAYERSTOSTARTGAME && i < 8; i++){                
                if (playerPool[i] != null && playerPool[i].isLooking){                   
                    gamePlayers[numGamePlayer] = playerPool[i];
                    playerPool[i].isLooking = false;
                    numOfPlayers--;
                    numGamePlayer++;
                }          
            }
            games[activeGames] = new Game(gamePlayers);
            games[activeGames].gameStart();
            activeGames++;
        }
    }
}
